import { Column, Entity, JoinTable, ManyToMany, OneToMany, Relation } from "typeorm";
import { MediaEntity } from "./media.entity";
import { CollectEntity } from "./collect.entity";
import { CommonEntity } from "./common.entity";
import { Exclude } from "class-transformer";
import { UploadHistoryEntity } from "./upload.entity";
import { ApiProperty } from "@nestjs/swagger";
import { RoleEntity } from "./role.entity";

@Entity({ name: "user" })
export class UserEntity extends CommonEntity {
    @ApiProperty({ description: "用户名" })
    @Column({ unique: true, length: 20 }) //设置唯一账号
    username: string;

    @ApiProperty({ description: "密码" })
    @Exclude()
    @Column()
    password: string;

    @ApiProperty({ type: [RoleEntity], description: "角色" })
    @ManyToMany(() => RoleEntity, (role) => role.users)
    @JoinTable({
        name: "users_roles",
        joinColumn: { name: "user_id", referencedColumnName: "id" },
        inverseJoinColumn: { name: "role_id", referencedColumnName: "id" },
    })
    roles: Relation<RoleEntity[]>;

    @Exclude()
    @OneToMany((type) => MediaEntity, (media) => media.user, { cascade: true })
    medias: MediaEntity[];

    @Exclude()
    @OneToMany((type) => CollectEntity, (collect) => collect.user, { cascade: true })
    collects: CollectEntity[];

    @Exclude()
    @OneToMany((type) => UploadHistoryEntity, (upload) => upload.user, { cascade: true })
    uploadHistorys: UploadHistoryEntity[];
}
